/**
  ETFAna project, Anyang Normal University && IMP-CAS
  \class ETFParticle
  \brief to store particle information of lighter fragments, e.g., proton,
  deutron, pion, etc.
  \author SUN Yazhou, asia.rabbit@163.com
  \since 2023-06-03
  \date 2023-06-06 last modified
  \attention
  changelog
  <table>
  <tr>  <th>Date         <th>Author      <th>Description                    </tr>
  <tr>  <td>2023-06-06   <td>Asia Sun    <td>file created                   </tr>
  </table>

  \copyright Copyright (c) 2021-2024 Anyang Normal U. && IMP-CAS with LGPLv3 LICENSE
*/

#ifndef ETFParticle_h
#define ETFParticle_h

#include <iostream>
#include <TObject.h>
#include "ETFMath.h"

using std::cout;
using std::endl;

class ETFParticle : public TObject{
public:
  ETFParticle();
  ~ETFParticle();
  ETFParticle(const ETFParticle &p) = default;
  ETFParticle &operator=(const ETFParticle &p) = default; ///< assignment ctor

  static const double DEG; // = ETFMath::DEGREE();
  static const double RAD; // = 1./DEG;
  static const double c0; // = ETFMath::c0();
  static const double CC; // = ETFMath::CC(); // 0.32184060506023993;
  static const double u0; // = ETFMath::uMeV(); // 931.494061
  static const double z1; // = ETFMath::zMagIn();
  static const double z2; // = ETFMath::zMagOut();

  int index; ///< the index of the event
  double aoz, poz;
  double z;
  double beta, gamma, bm2; // bm2 = beta^-2
  double tof2, trkLenT; ///< total flight path length
  double dtheta; ///< the deflection angle by the dipole magnet, in degree
  double rho, brho; ///< in SI unit
  double xo, zo; ///< (zo, xo): the center of the circle overlapping the arc
  double ki, bi; ///< for incident into the mag (in zOx plane)
  double ki2, bi2; ///< for incident into the mag (in zOy plane)
  double k1, b1; ///< for exiting the mag
  double k2, b2; ///< for exiting the mag
  double x1, x2; ///< the x of entrance and exit point the magField, respectively
  double pozp, pozt; ///< parallel and transverse momentum (w.r.t. the incident direction)
  int dcArrId; ///< 0: mpostMagTracker; 1: postMagTracker

  void Initialize();
  void Print(const char *opt = "") const override;
  double m() const{ return aoz*u0; } ///< in MeV
  double ai() const{ return atan(ki)*RAD; } ///< in degree
  double a1() const{ return atan(k1)*RAD; } ///< in degree
  double a2() const{ return atan(k2)*RAD; } ///< in degree
  double beta_(double dt) const{ return trkLenT / ((tof2 - dt)*c0); }
  double bg_(double dt) const{ return pow(bm2_(dt)-1., -0.5); }
  double bm2_(double dt) const{ return pow(beta_(dt), -2.); }
  double aoz_(double dt) const{ return brho*CC/bg_(dt); }
  double aoz_b(double k) const{
    double e = sqrt(ETFMath::sqr(poz/beta)+k/(beta*beta)); // add back the energy loss
    double m = fabs(aoz)*u0;
    // calculate beta using the added energy loss //
    double p = sqrt(e*e-m*m)*ETFMath::sign(poz);
    double bg = fabs(p) / m; // bg is now bigger

    // confirm the correction //
    m = poz/bg;
    double a = m/u0;

    return a;
  } // end aoz_b

  double y2() const{
    if(-999. == k2) return -999.;
    return k2*z2+b2;
  }

  ClassDefOverride(ETFParticle, 1); // a Particle info class

};

#endif
